/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist.delegate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.log4jconfig.xml.editor.utils.JdtUtils;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class LoggerContentAssistDelegatedProcessor extends
		AbstractContentAssistDelegatedProcessor {

	public void addAttributeValueProposals(
			ContentAssistRequest contentAssistRequest, String attributeName,
			String attributeCurrentValue, String attributeMatchValue)
			throws Exception {
		if ("name".equals(attributeName)) {
			IJavaProject javaProject = JdtUtils.getCurrentJavaProject();
			IPackageFragmentRoot[] packageFragmentRoots = javaProject
					.getAllPackageFragmentRoots();
			// FIXME : code below does not retrieve project's own classes when
			// package fragment is 'src',
			// so getClassFiles() returns empty list
			String matchingPackageValue = JdtUtils
				.getPackageFromQualifiedName(attributeCurrentValue);
			List<String> packagesProposals = new ArrayList<String>();
			List<String> classesProposals = new ArrayList<String>();

			for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots) {
				if (fragmentRoot.isOpen()) {
					for (IJavaElement element : fragmentRoot
							.getChildren()) {
						IPackageFragment fragment = (IPackageFragment)element;
						String name = fragment.getElementName();
						// exclude default package
						if (!name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
							packagesProposals.add(name);
						}

						// for current package only, add all the available
						// classes
						if (matchingPackageValue.equals(name)) {
							// package fragment with java classes
							if (fragment.getKind() == IPackageFragmentRoot.K_BINARY) {
								for (IClassFile classFile : fragment
										.getClassFiles()) {
									classesProposals.add(classFile.getType()
											.getFullyQualifiedName());
								}
							} 
							// package fragment with java source 
							//TODO : check if this also works with inner classes ?
							else {
								for (ICompilationUnit item : fragment
										.getCompilationUnits()) {
									classesProposals.add(item.findPrimaryType().getFullyQualifiedName());
								}
							}
						}
					}
				}
			}
			Collections.sort(packagesProposals);
			for (String packageProposal : packagesProposals) {
				addCompletionProposal(contentAssistRequest, packageProposal,
						attributeCurrentValue, attributeMatchValue,
						ISharedImages.IMG_OBJS_PACKAGE);
			}
			Collections.sort(classesProposals);
			for (String classProposal : classesProposals) {
				addCompletionProposal(contentAssistRequest, classProposal,
						attributeCurrentValue, attributeMatchValue,
						ISharedImages.IMG_OBJS_CLASS);
			}

		}

	}

}
